package top.lvkun.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import top.lvkun.basic.utils.AjaxResult;
import top.lvkun.basic.utils.RedisUtils;
import top.lvkun.order.domain.AdoptOrder;
import top.lvkun.order.service.IAdoptOrderService;
import top.lvkun.user.domain.User;

import javax.servlet.http.HttpServletRequest;

/**
 * 宠物领养订单接口
 */
@RestController
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    /**
     * 前台：用户领养宠物提交订单
     * @param adoptOrder
     * @param request
     * @return
     */
    @PutMapping("/adoptOrder")
    public AjaxResult submitAdoptOrder(@RequestBody AdoptOrder adoptOrder, HttpServletRequest request){
        try {
            User user = RedisUtils.INSTANCE.getUser(request, User.class);
            adoptOrder.setUser(user);
            return adoptOrderService.submitAdoptOrder(adoptOrder);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！我们正在殴打程序员"+e.getMessage());
        }
    }

}
